package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IInventorService;
import com.mdd.admin.validate.InventorCreateValidate;
import com.mdd.admin.validate.InventorUpdateValidate;
import com.mdd.admin.validate.InventorSearchValidate;
import com.mdd.admin.vo.InventorListedVo;
import com.mdd.admin.vo.InventorDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Inventor;
import com.mdd.common.mapper.InventorMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 发明人列实现类
 * @author LikeAdmin
 */
@Service
public class InventorServiceImpl implements IInventorService {
        
    @Resource
    InventorMapper inventorMapper;

    /**
     * 发明人列列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<InventorListedVo>
     */
    @Override
    public PageResult<InventorListedVo> list(PageValidate pageValidate, InventorSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Inventor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        inventorMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
            "like:chineseName@chinese_name:str",
            "like:englishName@english_name:str",
            "like:idNumber@id_number:str",
            "between:creatorTime@creator_time:str",
        });

        IPage<Inventor> iPage = inventorMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<InventorListedVo> list = new LinkedList<>();
        for(Inventor item : iPage.getRecords()) {
            InventorListedVo vo = new InventorListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public List<InventorListedVo> all(InventorSearchValidate searchValidate) {

        QueryWrapper<Inventor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        if (searchValidate.getCustomerId()!=null){
            queryWrapper.eq("customer_name_id", searchValidate.getCustomerId());
        }

        inventorMapper.setSearch(queryWrapper, searchValidate, new String[]{
                "=:userId@user_id:int",
        });

        List<Inventor> iPage = inventorMapper.selectList(queryWrapper);

        List<InventorListedVo> list = new LinkedList<>();
        for(Inventor item : iPage) {
            InventorListedVo vo = new InventorListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return list;
    }

    /**
     * 发明人列详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Inventor
     */
    @Override
    public InventorDetailVo detail(Integer id) {
        Inventor model = inventorMapper.selectOne(
                new QueryWrapper<Inventor>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        InventorDetailVo vo = new InventorDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 发明人列新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(InventorCreateValidate createValidate) {
        Inventor model = new Inventor();
        model.setCustomerNameId(createValidate.getCustomerNameId());
        model.setCustomerName(createValidate.getCustomerName());
        model.setChineseName(createValidate.getChineseName());
        model.setEnglishName(createValidate.getEnglishName());
        model.setJobNumber(createValidate.getJobNumber());
        model.setSurnameChinese(createValidate.getSurnameChinese());
        model.setNameChinese(createValidate.getNameChinese());
        model.setSurnameEnglish(createValidate.getSurnameEnglish());
        model.setNameEnglish(createValidate.getNameEnglish());
        model.setNationality(createValidate.getNationality());
        model.setIsTechnicist(createValidate.getIsTechnicist());
        model.setNation(createValidate.getNation());
        model.setProvince(createValidate.getProvince());
        model.setCityChinese(createValidate.getCityChinese());
        model.setCityEnglish(createValidate.getCityEnglish());
        model.setStreetChinese(createValidate.getStreetChinese());
        model.setStreetEnglish(createValidate.getStreetEnglish());
        model.setDepartmentChinese(createValidate.getDepartmentChinese());
        model.setDepartmentEnglish(createValidate.getDepartmentEnglish());
        model.setEmail(createValidate.getEmail());
        model.setPhone(createValidate.getPhone());
        model.setCertificateType(createValidate.getCertificateType());
        model.setIdNumber(createValidate.getIdNumber());
        model.setPostcode(createValidate.getPostcode());
        model.setLandlinePhone(createValidate.getLandlinePhone());
        model.setQq(createValidate.getQq());
        model.setCreatorId(createValidate.getCreatorId());
        model.setCreatorName(createValidate.getCreatorName());
        model.setCreatorTime(createValidate.getCreatorTime());
        model.setUpdateStaffId(createValidate.getUpdateStaffId());
        model.setUpdateStaff(createValidate.getUpdateStaff());
        model.setContentUpdateTime(createValidate.getContentUpdateTime());
        model.setRemark(createValidate.getRemark());
        inventorMapper.insert(model);
    }

    /**
     * 发明人列编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(InventorUpdateValidate updateValidate) {
        Inventor model = inventorMapper.selectOne(
                new QueryWrapper<Inventor>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCustomerNameId(updateValidate.getCustomerNameId());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setChineseName(updateValidate.getChineseName());
        model.setEnglishName(updateValidate.getEnglishName());
        model.setJobNumber(updateValidate.getJobNumber());
        model.setSurnameChinese(updateValidate.getSurnameChinese());
        model.setNameChinese(updateValidate.getNameChinese());
        model.setSurnameEnglish(updateValidate.getSurnameEnglish());
        model.setNameEnglish(updateValidate.getNameEnglish());
        model.setNationality(updateValidate.getNationality());
        model.setIsTechnicist(updateValidate.getIsTechnicist());
        model.setNation(updateValidate.getNation());
        model.setProvince(updateValidate.getProvince());
        model.setCityChinese(updateValidate.getCityChinese());
        model.setCityEnglish(updateValidate.getCityEnglish());
        model.setStreetChinese(updateValidate.getStreetChinese());
        model.setStreetEnglish(updateValidate.getStreetEnglish());
        model.setDepartmentChinese(updateValidate.getDepartmentChinese());
        model.setDepartmentEnglish(updateValidate.getDepartmentEnglish());
        model.setEmail(updateValidate.getEmail());
        model.setPhone(updateValidate.getPhone());
        model.setCertificateType(updateValidate.getCertificateType());
        model.setIdNumber(updateValidate.getIdNumber());
        model.setPostcode(updateValidate.getPostcode());
        model.setLandlinePhone(updateValidate.getLandlinePhone());
        model.setQq(updateValidate.getQq());
        model.setCreatorId(updateValidate.getCreatorId());
        model.setCreatorName(updateValidate.getCreatorName());
        model.setCreatorTime(updateValidate.getCreatorTime());
        model.setUpdateStaffId(updateValidate.getUpdateStaffId());
        model.setUpdateStaff(updateValidate.getUpdateStaff());
        model.setContentUpdateTime(updateValidate.getContentUpdateTime());
        model.setRemark(updateValidate.getRemark());
        inventorMapper.updateById(model);
    }

    /**
     * 发明人列删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Inventor model = inventorMapper.selectOne(
                new QueryWrapper<Inventor>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        inventorMapper.updateById(model);
    }

}
